package com.shawn.springboot.integeration.system.controller;

import com.shawn.springboot.integeration.common.config.shiro.token.JWTUtil;
import com.shawn.springboot.integeration.common.model.GenericResponse;
import com.shawn.springboot.integeration.system.model.dto.SysUserDTO;
import com.shawn.springboot.integeration.system.model.pojo.SysUser;
import com.shawn.springboot.integeration.system.service.ISysLoginService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * @author shawn
 * @description: 登录控制层
 * @date 2023/12/17
 */
@RestController
public class LoginController {

    @Resource
    private ISysLoginService loginService;

    @PostMapping("/login")
    public GenericResponse<String> login(@RequestBody SysUserDTO sysUser) {
        SysUser user = loginService.login(sysUser.getLoginName(), sysUser.getPassword());
        String token = JWTUtil.createToken(sysUser.getLoginName(), user.getPassword());
        return GenericResponse.success(token);
    }

    @RequestMapping(path = "/unauthorized/{message}")
    public GenericResponse<String> unauthorized(@PathVariable String message) throws UnsupportedEncodingException {
        return GenericResponse.error(message);
    }

    @GetMapping("/updatePassword")
    @RequiresRoles(logical = Logical.OR, value = {"user", "ADMIN"})
    public GenericResponse<String> updatePassword() {
//        String dataBasePassword = userMapper.getPassword(username);
//        if (dataBasePassword.equals(oldPassword)) {
//            userMapper.updatePassword(username, newPassword);
//        } else {
//            return resultMap.fail().message("密码错误！");
//        }
        return GenericResponse.success("成功获得信息！");
    }

    /**
     * 拥有 vip 权限可以访问该页面
     */
    @GetMapping("/getVipMessage")
    @RequiresRoles(logical = Logical.AND, value = {"user", "ADMIN"})
    public GenericResponse<String> getVipMessage() {
        return GenericResponse.success("成功获得 vip 信息！");
    }
}
